package com.kamloo.stg.data.db;

import java.util.ArrayList;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DirectionDAO {
	private SQLiteDatabase m_database;
	private MySQLiteHelper m_dbHelper;
	public static final String TABLE_NAME 	 	 = "direction";
	public static final String COLUMN_ID 		 = "_id";
	public static final String COLUMN_FK_LINE_ID = "fk_lineId";
	public static final String COLUMN_IDENTIFIER = "identifier";
	private String[] m_allColumns = { COLUMN_ID, COLUMN_FK_LINE_ID, COLUMN_IDENTIFIER };
	
	public static final String TABLE_CREATE = 
			"CREATE TABLE " + 
		    TABLE_NAME + "(" + 
		    COLUMN_ID + " INTEGER PRIMARY KEY, " + 
		    COLUMN_FK_LINE_ID + " INTEGER REFERENCES " + LineDAO.TABLE_NAME + "(" + LineDAO.COLUMN_ID + ") " +
		    		"ON UPDATE CASCADE ON DELETE CASCADE, " +
		    COLUMN_IDENTIFIER + " TEXT NOT NULL);";
	
	public DirectionDAO(Context context) {
		m_dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		m_database = m_dbHelper.getWritableDatabase();
	}

	public void close() {
		m_dbHelper.close();
	}
	
	
	public void add(Direction direction) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_ID, direction.getId());
		values.put(COLUMN_FK_LINE_ID, direction.getFk_lineId());
		values.put(COLUMN_IDENTIFIER, direction.getIdentifier());
		m_database.insert(TABLE_NAME, null, values);
	}
	
	public void delete(int id) {
		System.out.println("Direction deleted with id: " + id);
		m_database.delete(TABLE_NAME, COLUMN_ID + " = " + id, null);
	}
	
	public Direction select(int id) {
		Cursor cur = m_database.query(TABLE_NAME, m_allColumns, COLUMN_ID + " = " + id, 
									  null, null, null, null);
		
		if (cur != null)
			cur.moveToFirst();
		
		Direction newDirection = cursorToDirection(cur);
		cur.close();
		return newDirection;
	}
	
	public ArrayList<Direction> selectAll(){
		ArrayList<Direction> directions = new ArrayList<Direction>();
		
		Cursor cur = m_database.query(TABLE_NAME, m_allColumns, 
				  					  null, null, null, null, null);

		if (cur != null) {
			cur.moveToFirst();
			while (!cur.isAfterLast()) {
				directions.add(cursorToDirection(cur));
				cur.moveToNext();
			}
		}

		cur.close();
		return directions;
	}
	
	public ArrayList<Direction> selectFromLine(int lineId) {
		ArrayList<Direction> directions = new ArrayList<Direction>();
		
		Cursor cur = m_database.query(TABLE_NAME, m_allColumns, 
				  					  COLUMN_FK_LINE_ID + " = " + lineId, 
				  					  null, null, null, null);

		if (cur != null) {
			cur.moveToFirst();
			while (!cur.isAfterLast()) {
				directions.add(cursorToDirection(cur));
				cur.moveToNext();
			}
		}

		cur.close();
		return directions;
	}	
	
	public void update(Direction direction) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_FK_LINE_ID, direction.getFk_lineId());
		values.put(COLUMN_IDENTIFIER, direction.getIdentifier());
		m_database.update(TABLE_NAME, values, COLUMN_ID + " = " + direction.getId(), null);
	}	
	
	private Direction cursorToDirection(Cursor cursor) {
		return new Direction(cursor.getInt(0),
							 cursor.getInt(1),
							 cursor.getString(2));
	}
	
	
	
	
	/*	
	public static ArrayList<Direction> selectBeginEnd(int lineId, SQLiteDatabase db)
	{
		Cursor cur = db.query(TABLE_NAME, new String[] { COL_ID, COL_ID_ARRET, COL_ID_LIGNE }, 
														 String.format(WHERE, COL_ID_LIGNE, lineId),
														 null, null, null, null);
	}
	
	public static Direction select(int stopId, int lineId, SQLiteDatabase db) {
		Cursor cur = db.query(TABLE_NAME, new String[] { COL_ID, COL_ID_ARRET, COL_ID_LIGNE }, 
														 String.format(WHEREAND, COL_ID_ARRET, stopId, COL_ID_LIGNE, lineId),
														 null, null, null, null);
		
		if (cur != null)
			cur.moveToFirst();
		
		Direction direction = new Direction(cur.getInt(cur.getColumnIndex(COL_ID)), 
										    cur.getInt(cur.getColumnIndex(COL_ID_LIGNE)),
										    cur.getInt(cur.getColumnIndex(COL_ID_ARRET)));
		cur.close();
		return direction;
	}
	*/
}
